package com.example.finalproject.experiments.guessgame

import android.os.Bundle
import android.widget.Button
import android.widget.RadioGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.finalproject.R

class GuessGameActivity : AppCompatActivity() {

    private lateinit var userRadioGroup: RadioGroup
    private lateinit var computerRadioGroup: RadioGroup
    private lateinit var determineButton: Button
    private lateinit var resultTextView: TextView
    private lateinit var testResultTextView: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_guess_game)

        userRadioGroup = findViewById(R.id.userRadioGroup)
        computerRadioGroup = findViewById(R.id.computerRadioGroup)
        determineButton = findViewById(R.id.determineButton)
        resultTextView = findViewById(R.id.resultTextView)
        testResultTextView = findViewById(R.id.testResultTextView)

        determineButton.setOnClickListener {
            val userChoiceId = userRadioGroup.checkedRadioButtonId
            val computerChoiceId = computerRadioGroup.checkedRadioButtonId

            if (userChoiceId == -1 || computerChoiceId == -1) {
                resultTextView.text = "请选择双方的出拳！"
                return@setOnClickListener
            }

            val userChoice = when (userChoiceId) {
                R.id.userStone -> "石头"
                R.id.userScissors -> "剪刀"
                R.id.userPaper -> "布"
                else -> ""
            }

            val computerChoice = when (computerChoiceId) {
                R.id.computerStone -> "石头"
                R.id.computerScissors -> "剪刀"
                R.id.computerPaper -> "布"
                else -> ""
            }

            val result = determineResult(userChoice, computerChoice)
            resultTextView.text = "你出了：$userChoice\n电脑出了：$computerChoice"
            testResultTextView.text = "结果：$result"
        }
    }

    private fun determineResult(userChoice: String, computerChoice: String): String {
        return when {
            userChoice == computerChoice -> "平局"
            (userChoice == "石头" && computerChoice == "剪刀") || (userChoice == "剪刀" && computerChoice == "布") || (userChoice == "布" && computerChoice == "石头") -> "你赢了"
            else -> "你输了"
        }
    }
}